$('.product').livequery(function() {
	//Показ/скрытие формы отзывов
	$('.add-review').on('click', function() {
		$(this).hide();
		$('.add-review-wrap').slideDown(200);
		return false;
	});
	
	$('.add-review-block .cancel-s-review').on('click', function() {
		$('.add-review-wrap').slideUp(200, function(){
			clearReviewForm();
		});
	});
	
	$('.add-review-block .traps span').mouseenter(function() {
		var number = $(this).index() + 1;
		$('.add-review-block .b-stars-value').css({'width' : 100 * (number / 5) + '%'});
	})
	.mouseleave(function() {
		var number = $('.add-review-block .stars').attr('val');
		if(number) $('.add-review-block .b-stars-value').css({'width' : 100 * (number / 5) + '%'});
		else $('.add-review-block .b-stars-value').css({'width' : 0 + '%'});
	})
	.click(function(){
		var number = $(this).index() + 1;
		$(this).closest('.add-review-block .stars').attr('val', number);
	});
	
	function clearReviewForm() {
		$('.add-review-block .stars').removeAttr('val');
		$('.add-review-block textarea').val('');
		$('.add-review-block .b-stars-value').css({'width' : 0 + '%'});
		$('.reviews .add-review').show();
	}
	
	//Сохранение отзыва
	$('.save-review').on('click', function() {
		var self = $(this);
		if(self.hasClass('blocked')) return false;
		
		$('.add-review-block .val-err').html('');
		var isSuccess = true;
		var scores = $('.add-review-block .stars').attr('val');
		var reviewText = $('#review-text').val();
		var idProduct = $('.buy').data('product-id');
			
		if(!scores)  {
			$('.add-review-block .val-err').append('<div>Поставте оценку товару</div>');
			isSuccess = false;
		}
		if(!reviewText || trim(reviewText) == '') {
			$('.add-review-block .val-err').append('<div>Заполните текст отзыва</div>');
			isSuccess = false;
		}
		else if(reviewText.length > 2000) {
			$('.add-review-block .val-err').append('<div>Слишком длинный отзыв</div>');
			isSuccess = false;
		}

		if(isSuccess) {
			self.addClass('blocked');
			$.ajax({  
				type: "POST",  
				url: W_AJAX + "review/add",  
				data: {
					text: reviewText,
					score: scores,
					ID_product: idProduct
				},
				dataType: "json",
				success: function(objData) {
					if (objData.result == 'OK') window.location.reload();
					else App.showError(objData.error);
					self.removeClass('blocked');
				}
			});
		}
		else if(!$('.add-review-block .val-err').is(":visible")) {
			$('.add-review-block .val-err').clearQueue().slideDown();
			setTimeout(function() { $('.add-review-block .val-err').slideUp();}, 2500);
		}
		
		return false;
	});
});